/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import FrameWork.*;
import FrameWork.Audio.RSAudio;
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author DANTRUONG
 */
public class State_About extends IState{
    Sprite m_Dialog;
    Sprite m_Ball;
    int m_IDChoosed;
    
    Rectangle m_RectButton;
    Rectangle m_RectNext;
    Rectangle m_RectPre;
    
    float m_SaveTime;
    MySpriteFont m_Font;
    
    Sprite m_about;
    Sprite m_aboutus;
    Sprite m_intro;
    Sprite m_home;
    Sprite m_next;
    
    int m_CurPage;
    int m_NextPage;
    final int m_TotalPage=3;
    float m_deta;
    
    public State_About(IPlay _Play){
        super(_Play);
        m_IDState = Global.STATE_ABOUT;
    }
    
    
    public void Init (){
        super.Init();
        try {
            m_about = new  Sprite (
                                Image.createImage("/Image/about.png"),
                                240, 320);
            m_aboutus = new  Sprite (
                                Image.createImage("/Image/aboutus.png"),
                                240, 320);
            m_intro = new  Sprite (
                                Image.createImage("/Image/intro.png"),
                                240, 320);
            m_home = new  Sprite (
                                Image.createImage("/Image/home.png"),
                                42, 42);
            m_next = new  Sprite (
                                Image.createImage("/Image/next.png"),
                                36, 48);
            m_Dialog = new Sprite ( Image.createImage("/Image/dialog.png"),
                            204, 235);
            m_Ball = new Sprite (Image.createImage("/Image/ball1.png"),
                            30, 30);
            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
                
        m_IDChoosed = 0;
        
        m_RectButton = new Rectangle(90, 255, 60, 60);
        m_RectNext   = new Rectangle(171, 255, 60, 60);
        m_RectPre    = new Rectangle(10, 255, 60, 60);
        
        m_SaveTime = 0;
        m_Font = RSFont.getRS().getFont(Global.FONT_SMALL);
//        m_Status = 0;
        m_CurPage = 0;
        m_NextPage = 0;
        
        m_deta = 0;
    }
    
    public void PointerPressed(int x, int y){
        System.out.println("Cur:"+m_CurPage+ "-- next:"+m_NextPage);
        if(m_RectButton.Intersects(new Point(x,y))){
                m_IDChoosed = 1;
                RSAudio.getRS().PlayExplosion();
                m_Play.setNextState(new State_Menu(m_Play));
            }
        if(m_CurPage == m_NextPage){
            
            if(m_CurPage>0){
                if(m_RectPre.Intersects(new Point(x,y))){
                    m_NextPage = m_CurPage -1;
                    m_IDChoosed = 2;
                    RSAudio.getRS().PlayBubble();
                }
            }
       
           if(m_CurPage< m_TotalPage-1){
                if(m_RectNext.Intersects(new Point(x,y))){
                    m_NextPage = m_CurPage +1;
                    m_IDChoosed = 3;
                    RSAudio.getRS().PlayBubble();
                }
           }
            
        }
    }
    
    public void KeyPressed (int _Keycode){
        if (_Keycode == -5 || _Keycode == 53){
            RSAudio.getRS().PlayExplosion();
            m_IDChoosed = 1;
            m_Play.setNextState(new State_Menu(m_Play));
            
        }
        
        if(m_CurPage == m_NextPage){
            
            if(m_CurPage>0){
                 if (_Keycode == -3 || _Keycode == 52){
                    m_NextPage = m_CurPage -1;
                    m_IDChoosed = 2;
                    RSAudio.getRS().PlayBubble();
                }
            }
       
           if(m_CurPage< m_TotalPage-1){
                if (_Keycode == -4 || _Keycode == 54){
                    m_NextPage = m_CurPage +1;
                    m_IDChoosed = 3;
                    RSAudio.getRS().PlayBubble();
                }
           }
            
        }
    }
    
    public void Update (float _Time){
        System.out.println("Deta:"+m_deta);
        if(m_CurPage != m_NextPage){
            m_deta+= _Time *500;
            if(m_deta>=240){
                m_deta =0;
                m_IDChoosed = 0;
                m_CurPage = m_NextPage;
            }
        }
    }
    
    public void Render (){
       super.Render();
       m_Graphics.setColor(0, 0, 0);
       m_Graphics.fillRect(0, 0, 240, 320);
       
       
       m_home.setPosition(98, 265);
       m_home.paint(m_Graphics);
       
       if(m_IDChoosed==1){
             m_Ball.setFrame(1);
             m_Ball.setPosition(20 + 70, 255);
             m_Ball.paint(m_Graphics);
        }else {
             m_Ball.setFrame(0);
             m_Ball.setPosition(20 + 70, 255);
             m_Ball.paint(m_Graphics);
        }
       
       if(m_CurPage>0){
             m_next.setTransform(Sprite.TRANS_NONE);
            m_next.setPosition(20, 262);
            m_next.paint(m_Graphics);
            
           if(m_IDChoosed==2){
               m_Ball.setFrame(1);
           }else{
               m_Ball.setFrame(0);
           }
            m_Ball.setPosition(10, 255);
            m_Ball.paint(m_Graphics);
          
       }
       
       if(m_CurPage<m_TotalPage-1){
            m_next.setTransform(Sprite.TRANS_MIRROR);
            m_next.setPosition(184, 262);
            m_next.paint(m_Graphics);
            
           if(m_IDChoosed==3){
               m_Ball.setFrame(1);
           }else{
               m_Ball.setFrame(0);
           }
            m_Ball.setPosition(171, 255);
            m_Ball.paint(m_Graphics);
           
       }
       
       
//       m_next.setRefPixelPosition(m_IDState, m_IDState);
        
       
        if(m_CurPage!= m_NextPage){
            int xcur;
            int xnext;
            if(m_CurPage<m_NextPage){
              xcur = 0-(int)m_deta;
              xnext = 240-(int)m_deta;
                             
            }else{
                 xcur = 0+(int)m_deta;
                 xnext = -240+(int)m_deta;
            }
            
            //<editor-fold defaultstate="collapsed" desc="Curpage">
            m_Dialog.setFrame(0);
            m_Dialog.setPosition(18+xcur, 15);
            m_Dialog.paint(m_Graphics);
            switch(m_CurPage){
                case 0:
                    m_intro.setPosition(xcur, 0);
                    m_intro.paint(m_Graphics);
                    break;
                case 1:
                    m_about.setPosition(xcur, 0);
                    m_about.paint(m_Graphics);
                    break;
                case 2:
                    m_aboutus.setPosition(xcur, 0);
                    m_aboutus.paint(m_Graphics);
                    break;
            }
            //</editor-fold>
            
            //<editor-fold defaultstate="collapsed" desc="Nextpage">
            m_Dialog.setFrame(0);
            m_Dialog.setPosition(18+xnext, 15);
            m_Dialog.paint(m_Graphics);
            switch(m_NextPage){
                case 0:
                    m_intro.setPosition(xnext, 0);
                    m_intro.paint(m_Graphics);
                    break;
                case 1:
                    m_about.setPosition(xnext, 0);
                    m_about.paint(m_Graphics);
                    break;
                case 2:
                    m_aboutus.setPosition(xnext, 0);
                    m_aboutus.paint(m_Graphics);
                    break;
            }
            //</editor-fold>
            
        }else{
            
            //<editor-fold defaultstate="collapsed" desc="Curpage">
            m_Dialog.setFrame(0);
            m_Dialog.setPosition(18, 15);
            m_Dialog.paint(m_Graphics);
            switch(m_CurPage){
                case 0:
                    m_intro.setPosition(0, 0);
                    m_intro.paint(m_Graphics);
                    break;
                case 1:
                    m_about.setPosition(0, 0);
                    m_about.paint(m_Graphics);
                    break;
                case 2:
                    m_aboutus.setPosition(0, 0);
                    m_aboutus.paint(m_Graphics);
                    break;
            }
            //</editor-fold>
        }
        
    }
    public void Destroy (){
        
        m_Dialog = null;
        m_Ball= null;    
         m_RectButton= null;
         m_RectNext= null;
         m_RectPre= null;

         m_Font= null;

         m_about= null;
         m_aboutus= null;
         m_intro= null;
         m_home= null;
         m_next= null;
        
    }
}
